package graphicinterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * this class will draw the first interface the user will see on our system.
 * When the user selects an option from this screen, it'll create other screens
 * @author Mina
 *
 */
public class WindowInitialScreen implements MouseListener
{
	private JFrame windowInitialScreen;
	private JPanel panelBookRecommendationsByTag;
	private JPanel panelRecentReviewsByTag;
	private JPanel panelBestAuthorsByTag;
	private JPanel panelHowLongToFinishBook;
	
	public WindowInitialScreen()
	{
		this.windowInitialScreen = new JFrame("bookworm book reviews");
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		this.windowInitialScreen.setSize(d);
		this.windowInitialScreen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.windowInitialScreen.setAlwaysOnTop(true);
		drawInitialScreen();
	}
	
	private void drawInitialScreen()
	{
		JPanel panelInitialScreen = new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		GridBagLayout layout = new GridBagLayout();
		panelInitialScreen.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		
		ImageIcon mainMascotIcon = 
				CreatesImageIcon.createImageIcon("images/whatdoyouwanttodo.png",
											"main mascot asks what the user would like to do");
		JLabel mainMascotLabel = new JLabel(mainMascotIcon);
		grid.gridheight = 1;
		grid.gridwidth = 2;
		panelInitialScreen.add(mainMascotLabel, grid);
		
		this.panelBookRecommendationsByTag = 
				createPanelSystemFunctionality("get books recommendations by tag",
																"images/mascotbooksbytag.png");
		this.panelBookRecommendationsByTag.addMouseListener(this);
		grid.gridwidth = 1;
		grid.gridy = 1;
		panelInitialScreen.add(panelBookRecommendationsByTag, grid);
		
		this.panelRecentReviewsByTag = 
				createPanelSystemFunctionality("get recent book reviews by tag", 
															"images/mascotrecentbooksbytag.png");
		this.panelRecentReviewsByTag.addMouseListener(this);
		grid.gridx = 1;
		grid.gridy = 2;
		panelInitialScreen.add(panelRecentReviewsByTag, grid);
		
		this.panelBestAuthorsByTag = 
				createPanelSystemFunctionality("get best authors by tag", 
																"images/mascotauthorsbytag.png");
		this.panelBestAuthorsByTag.addMouseListener(this);
		grid.gridx = 0;
		grid.gridy = 3;
		panelInitialScreen.add(panelBestAuthorsByTag, grid);
		
		this.panelHowLongToFinishBook = 
				createPanelSystemFunctionality("how long to finish book?", 
															"images/mascothowlongfinishbook.png");
		this.panelHowLongToFinishBook.addMouseListener(this);
		grid.gridx = 1;
		grid.gridy = 4;
		panelInitialScreen.add(panelHowLongToFinishBook, grid);
		
		
		this.windowInitialScreen.add(panelInitialScreen);
		this.windowInitialScreen.setVisible(true);
	}
	
	
	/**
	 * this function will create a JPanel with a mascot and a text.
	 * Each of the system functionalities that will be presented to the user will have
	 * it's own JPanel with mascot and text created by this funtion.
	 * @param nameOfFunctionality
	 * @param whereIsMascotIcon
	 * @return
	 */
	private JPanel createPanelSystemFunctionality(String nameOfFunctionality,
																String whereIsMascotIcon)
	{
		ImageIcon mascotIcon = 
				CreatesImageIcon.createImageIcon(whereIsMascotIcon, "one mascot image");
		JLabel labelMascot = new JLabel(mascotIcon);
		JLabel labelSystemFunctionality =
				TextLabelCreator.createStyledTextlabel(nameOfFunctionality);
		JPanel panelSystemFunctionality = new JPanel();
		panelSystemFunctionality.setBackground(new Color(255,131,137));
		panelSystemFunctionality.add(labelMascot);
		panelSystemFunctionality.add(labelSystemFunctionality);
		
		return panelSystemFunctionality;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		Component objectClicked = arg0.getComponent();
		String whichWindowShouldComeNext; 
		/*if the user selects an option that will ask him to choose tags, this string will
		   tell the "InterfaceChooseBookTag" which window should come after he chooses a tag*/
		boolean disposeInitialScreenWindow = true;
		/*
		 * the boolean above represents if the initial screen can be disposed after mouse click.
		 * by default, this window can be disposed. But without Internet connection,
		 * for example, the user will have to choose another feature if he/she selects a
		 *  feature that requires Internet connection
		 */
		if(objectClicked == this.panelBestAuthorsByTag)
		{
			whichWindowShouldComeNext = "InterfaceBestAuthorsBasedOnTag";
			WindowChooseBookTag windowChooseTag =
					new WindowChooseBookTag(whichWindowShouldComeNext);
		}
		else if(objectClicked == this.panelBookRecommendationsByTag)
		{
			whichWindowShouldComeNext = "InterfaceBooksFromTag";
			WindowChooseBookTag windowChooseTag =
					new WindowChooseBookTag(whichWindowShouldComeNext);
		}
		else if(objectClicked == this.panelRecentReviewsByTag)
		{
			/* 
			 * this button leads to a feature that requires Internet connection. Is the computer
			 * connected to the Internet? Let's ask the facade!
			 */
			boolean computerConnectedToInternet = SystemFunctionalitiesFacade.getInstance().computerIsConnectedToInternet();
			if(computerConnectedToInternet == true)
			{
				whichWindowShouldComeNext = "InterfaceRecentReviewsByTag";
				WindowChooseBookTag windowChooseTag =
						new WindowChooseBookTag(whichWindowShouldComeNext);
			}
			else
			{
				//computer not connected to Internet. Let''s warn the user that he can't use this feature.
				WindowNoInternetConnection windowNoConnection = new WindowNoInternetConnection();
				disposeInitialScreenWindow = false;//the user will have to choose another option.
			}
		}
		else
		{
			/* 
			 * this button leads to a feature that requires Internet connection. Is the computer
			 * connected to the Internet? Let's ask the facade!
			 */
			boolean computerConnectedToInternet = SystemFunctionalitiesFacade.getInstance().computerIsConnectedToInternet();
			if(computerConnectedToInternet == true)
			{
				//no need to draw window that will ask for tag. User selected how long finish book
				WindowHowLongToFinishBook windowHowLongFinishBook = 
						new WindowHowLongToFinishBook();
			}
			else
			{
				//computer not connected to Internet. Let''s warn the user that he can't use this feature.
				WindowNoInternetConnection windowNoConnection = new WindowNoInternetConnection();
				disposeInitialScreenWindow = false;//the user will have to choose another option.
			}
		}
		if(disposeInitialScreenWindow == true)
		{
			this.windowInitialScreen.dispose(); //dispose the current window
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) 
	{
		Component objectEntered = arg0.getComponent();
		objectEntered.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
